<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Blacklist;
use app\common\model\Room;
use app\common\model\RoomChat;
use app\common\model\RoomUser;
use app\common\model\UserChat;
use app\common\model\UserChatGroup;
use think\Db;

class Chat extends Api{

    //protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    /**
     * 获取聊天列表
     */
    public function getChatList(){

        $page = request()->post('page',1);
        $limit = request()->post('limit',config('paginate.list_rows'));
        // 获取我所聊天的所有用户分组

        $groupIds = UserChatGroup::where('user_id',$this->auth->id)->whereOr('to_user_id',$this->auth->id)->column('id');
        // 由于无法分组后排序。所以用子查询。先获取子查询sql

        $subQuery  = UserChat::where('group_id','IN',$groupIds)->order(['created_at'=>'desc'])->limit(99999)->buildSql();
        // 查出所有用户与我的最新聊天记录

        $page = ($page-1) * $limit;
        $blacklist = Blacklist::where('user_id',$this->auth->id)->column('to_user_id');
        //echo $page;die;
        $userChatList = Db::table($subQuery . "user_chat")
            ->join('user u','u.id=user_chat.user_id')
            ->join('user tu','tu.id=user_chat.to_user_id')
            ->where('user_chat.user_id','NOT IN',$blacklist)
            ->where('user_chat.to_user_id','NOT IN',$blacklist)
            //->where('tu.id','NOT IN',$blacklist)
            ->where('user_chat.group_id','IN',$groupIds)
            ->group('user_chat.group_id')
            ->order(['user_chat.created_at'=>'desc'])
            ->field('user_chat.*,u.nickname,u.avatar,tu.nickname as to_nickname,tu.avatar as to_avatar')
            ->limit($page,$limit)
            ->select();

        $this->success('请求成功',$userChatList);
    }

    /**
     * 获取我的群聊列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRoomChatList(){
        //// 获取我所聊天的所有房间
        //$roomIds = RoomUser::where('user_id',$this->auth->id)->column('room_id');
        //// 由于无法分组后排序。所以用子查询。先获取子查询sql
        //$subQuery  = RoomChat::where('room_id','IN',$roomIds)->order(['created_at'=>'desc'])->limit(99999)
        //    ->buildSql();
        //// 查出所有房间最新聊天记录
        //$roomChatList = Db::table($subQuery . "room_chat")
        //    ->join('user u','u.id=room_chat.user_id')
        //    ->join('room r','r.id=room_chat.room_id')
        //    ->where('room_chat.room_id','IN',$roomIds)
        //    ->group('room_chat.room_id')
        //    ->order(['room_chat.created_at'=>'desc'])
        //    ->field('room_chat.*,u.nickname,r.name,r.user_id as group_owner_id,r.image')
        //    ->select();
        $roomIds = RoomUser::where('user_id',$this->auth->id)->column('room_id');

        $roomList = Room::where('id','IN',$roomIds)->field('id,name,image,user_id,user_count')->select();
        foreach($roomList as $v){
            $v->roomchat = RoomChat::with('user')->where('room_id',$v['id'])->order('created_at','desc')->limit(1)->find();
        }
        $this->success('请求成功',$roomList);
    }

    /**
     * 聊天记录
     */
    public function log(){
        $userId = request()->post('user_id');
        $roomId = request()->post('room_id');
        $lastChatId = request()->post('last_chat_id',0);
        $page = request()->post('page');
        $limit = request()->post('limit');
        if((empty($userId) && empty($roomId))){
            $this->error('参数错误');
        }
        if(empty($userId)){
            $list = RoomChat::getList($roomId,$lastChatId,$page,$limit);
        }else{
            $list = UserChat::getList($this->auth->id,$userId,$lastChatId,$page,$limit);
        }

        $this->success('请求成功',$list);
    }

    /**
     * 获取群成员列表
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRoomUserList(){
        $roomId = request()->post('roomId');
        $page = request()->post('page',1);
        $limit = request()->post('limit',config('paginate.list_rows'));
        $count = RoomUser::where('user_id',$this->auth->id)->where('room_id',$roomId)->count();
        if($count < 1){
            $this->error('请求错误');
        }
        $count = RoomUser::where('room_id',$roomId)->count();
        $userIds = RoomUser::where('room_id',$roomId)->page($page,$limit)->column('user_id');
        $userList = \app\common\model\User::where('id','IN',$userIds)->field('id,nickname,avatar,level,seniority,education')->select();
        $this->success('请求成功',[['total'=>$count,'user_id'=>Room::where('id',$roomId)->value('user_id')],'list'=>$userList]);
    }

    /**
     * 修改群聊信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editRoomInfo(){
        $roomId = request()->post('room_id');
        $name = request()->post('name');
        $image = request()->post('image');
        if(empty($roomId) || empty($name) || empty($image)){
            $this->error('参数错误');
        }
        $info = Room::where('id',$roomId)->where('user_id',$this->auth->id)->find();
        if(empty($info)){
            $this->error('参数错误');
        }

        Room::where('id',$roomId)->update([
            'name'  => $name,
            'image' => $image
        ]);
        $this->success('修改成功');
    }
}